﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Workplatm.model;
using wxt;

namespace Workplatm
{
    /// <summary>
    /// Interaction logic for Window_ChoseDMMC.xaml
    /// </summary>
    public partial class Window_ChoseZyxx : Window
    {
        LoginInfo loginInfo = LoginInfo.getInstance();
        String searchText = "";
        String sqlZyxx = "";
        int[] fj = null;
        DataSet ds_zyxx = null;
        DataSet ds_bmxx = null;
        String resultInfo = "";
        public String dm = "";
        public String mc = "";
        public String bmdm = "";
        public String bmmc = "";
        public String dmmc = "";
        public Window_ChoseZyxx(String sqlFjfa, char charFj, String sqlData,String searchText)
        {
            InitializeComponent(); 
            this.searchText = searchText;
            this.text_search.TxtValue = this.searchText;
            intData(sqlFjfa, charFj, sqlData);
             
        }
        //初始化数据
        private void intData(String sqlFjfa, char charFj, String sqlData)
        {
            //获取代码分级方案 
            DataSet dsfj = Dao.getSql(sqlFjfa, "GL_FZXLB", out resultInfo);
            if (dsfj != null && dsfj.Tables.Count > 0)
            {
                foreach (DataRow row in dsfj.Tables[0].Rows)
                {
                    String lbfj = row[0].ToString();
                    String[] fjTemp = lbfj.Split(charFj);
                    fj = new int[fjTemp.Length];
                    for (int i = 0; i < fjTemp.Length; i++)
                    {
                        fj[i] = Int16.Parse(fjTemp[i]);
                    }
                }
            }
            //填充信息
            DataSet ds = Dao.getSql(sqlData, "gl_xmzl", out resultInfo);
            resetData(ds);
            //查询数据库中的职员信息
            String sql = "select zydm,zyxm,zy.bmdm,bm.bmmc from (select zydm,zyxm,bmdm from pubzyxx where gsdm = '" + this.loginInfo.Hsdwdm + "' and kjnd = '" + this.loginInfo.Dywrq.Year + "'  and bmdm  like '" + this.bmdm + "%'" + " ) zy left join (select bmdm,bmmc from pubbmxx where gsdm = '" + this.loginInfo.Hsdwdm + "' and kjnd = '" + this.loginInfo.Dywrq.Year + "')  bm on   zy.bmdm = bm.bmdm ";
            this.ds_zyxx = Dao.getSql(sql, "pubzyxx", out resultInfo);
            resetZyxx(this.ds_zyxx);
        }
        //重置部门信息 用于搜索
        private void resetData(DataSet ds ) {
            this.treeView_dmmc.Items.Clear();
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    int preLen = 0;
                    String preDM = "";
                    String xmdm = row[0].ToString();
                    String xmmc = row[1].ToString();
                    for (int i = 0; (i < fj.Length && preLen < xmdm.Length); i++)
                    {
                        if ((preLen + fj[i]) >= xmdm.Length)
                        {
                            break;
                        }
                        else
                        {
                            preLen += fj[i];
                        }
                    }
                    preDM = xmdm.Substring(0, preLen);
                    TreeViewItem tvi = new TreeViewItem();
                    tvi.Name = "name_" + xmdm;
                    tvi.Header = xmdm + "-" + xmmc;
                    tvi.MouseDoubleClick += Tvi_MouseDoubleClick;
                    Boolean hasPre = false;
                    ItemCollection itemConllection = this.treeView_dmmc.Items;
                    loopTreeViewNode(itemConllection, tvi, preDM, fj);
                }
            }
        }
        //重置职员信息 用于搜索
        private void resetZyxx(DataSet ds) { 
                this.dg_zyxx.ItemsSource = null;
                String resultInfo = "";
                if (ds != null && ds.Tables.Count > 0)
                {
                    List<Zyxx> listZyxx = new List<Zyxx>();
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        Zyxx zyxx = new Zyxx();
                        zyxx.Zydm = row["zydm"].ToString();
                        zyxx.Bmdm = row["bmdm"].ToString();
                        zyxx.Bmmc = row["bmmc"].ToString();
                        zyxx.Zyxm = row["zyxm"].ToString();
                        zyxx.Bmxx = zyxx.Bmdm + "-" + zyxx.Bmmc;
                        if (zyxx.HasData)
                        {
                            listZyxx.Add(zyxx);
                        }
                    }
                    this.dg_zyxx.ItemsSource = listZyxx;
                }
            
        }
        //双击节点名称事件
        private void Tvi_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.treeView_dmmc.SelectedItem != null)
            {
                this.dg_zyxx.ItemsSource = null;
                this.bmdm = (this.treeView_dmmc.SelectedItem as TreeViewItem).Name.Substring(5);
                this.bmmc = (this.treeView_dmmc.SelectedItem as TreeViewItem).Header.ToString();
                this.bmmc.Substring(this.bmmc.IndexOf("-") >= 0 ? this.bmmc.IndexOf("-") : 0);
                String resultInfo = "";
                DataSet ds_temp = this.ds_zyxx.Copy();
                ds_temp.Tables[0].Rows.Clear();
                foreach (DataRow row in this.ds_zyxx.Tables[0].Rows) {
                    if (row[2].ToString().IndexOf(this.bmdm) >= 0) {
                        String zydm = row[0].ToString();
                            String zyxm = row[1].ToString();
                        String bmdm= row[2].ToString();
                        String bmmc= row[3].ToString();
                        ds_temp.Tables[0].Rows.Add(zydm,zyxm,bmdm,bmmc); 
                    }
                } 
                resetZyxx(ds_temp);
            }
        }

        //循环查询节点
        private Boolean loopTreeViewNode(ItemCollection itemConllection,TreeViewItem node,String preDM,int [] fj)
        {
            Boolean hasPre = false;
            try
            {
                foreach (TreeViewItem tviTemp in itemConllection)
                {
                    if (tviTemp.Name.Equals("name_" + preDM))
                    { 
                        tviTemp.Items.Add(node);
                        hasPre = true;
                        break;
                    }
                    else if (node.Name.Substring(0,tviTemp.Name.Length).Equals(tviTemp.Name) && tviTemp.HasItems && !hasPre)
                    {
                        hasPre = loopTreeViewNode(tviTemp.Items, node, preDM, fj);
                    }
                    else
                    {
                        continue;
                    }
                }
                if (!hasPre)
                { 
                    this.treeView_dmmc.Items.Add(node);
                }
                return hasPre;
            }
            catch (Exception e1)
            {  
                return hasPre;
            }
        }
           //搜索按钮事件
        private void btn_search_Click(object sender, RoutedEventArgs e)
        { 
            this.searchText = this.text_search.TxtValue;
            if (searchText.Equals("")) { 
                resetZyxx(this.ds_zyxx);
                return;
            }  
            DataSet dsTemp = this.ds_zyxx.Copy();
            dsTemp.Clear();
            foreach (DataRow row in  this.ds_zyxx.Tables[0].Rows) {
                String dm = row[0].ToString();
                String mc = row[1].ToString();
                String bmdm = row[2].ToString();
                String bmmc = row[3].ToString();
                if (dm.IndexOf(searchText) >= 0 || mc.IndexOf(searchText) >= 0 || (dm + "-" + mc).IndexOf(searchText) >= 0)
                {
                    dsTemp.Tables[0].Rows.Add(dm, mc, bmdm, bmmc);
                }
            }
            resetZyxx(dsTemp);
        }
        //确认带回数据按钮事件
        private void btn_selected_Click(object sender, RoutedEventArgs e)
        {
            if (this.dg_zyxx.SelectedItem != null) {
                Zyxx zyxx = (this.dg_zyxx.SelectedItem as Zyxx);
                this.dm = zyxx.Zydm;
                this.mc = zyxx.Zyxm;
                this.dmmc = this.dm + "-" + this.mc;
                this.DialogResult = true;
            } 
        }
        //搜索框中回车激活查询功能
        private void text_search_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Enter))
            {
                this.searchText = this.text_search.TxtValue;
                if (searchText.Equals(""))
                {
                    resetZyxx(this.ds_zyxx);
                    return;
                }
                DataSet dsTemp = this.ds_zyxx.Copy();
                dsTemp.Clear();
                foreach (DataRow row in this.ds_zyxx.Tables[0].Rows)
                {
                    String dm = row[0].ToString();
                    String mc = row[1].ToString();
                    String bmdm = row[2].ToString();
                    String bmmc = row[3].ToString();
                    if (dm.IndexOf(searchText) >= 0 || mc.IndexOf(searchText) >= 0 || (dm+"-"+mc).IndexOf(searchText)>=0)
                    { 
                        dsTemp.Tables[0].Rows.Add(dm, mc, bmdm,bmmc);
                    }
                }
                resetZyxx(dsTemp);
            }
        }

        //双击数据行返回信息
        private void dg_zyxx_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.dg_zyxx.SelectedItem != null)
            {
                Zyxx zyxx = (this.dg_zyxx.SelectedItem as Zyxx);
                this.dm = zyxx.Zydm;
                this.mc = zyxx.Zyxm;
                this.dmmc = this.dm + "-" + this.mc;
                this.DialogResult = true;
                this.Hide();
            }
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
